﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_Standard.cs
* 创建标识：曾乃强 15158756652 2011-03-17 11:03:01
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_Standard*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_Standard
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_Standard obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "Standard ([StandardName],[ShowWay],[StandardOtherName],[StandardType],[StandardDetail])");
            sql.Append("values(@StandardName,@ShowWay,@StandardOtherName,@StandardType,@StandardDetail)");
            sql.Append(";SELECT @@IDENTITY");
            SqlParameter[] pra = {
                            new SqlParameter("@StandardName",obj.StandardName),
							new SqlParameter("@ShowWay",obj.ShowWay),
							new SqlParameter("@StandardOtherName",obj.StandardOtherName),
							new SqlParameter("@StandardType",obj.StandardType),
							new SqlParameter("@StandardDetail",obj.StandardDetail)
                                 };
            return Convert.ToInt32(DbHelper.ExecuteScalar(sql.ToString(), pra));
        }

        public int Update(Model.CNVP_Standard obj)
        {
             StringBuilder sql = new StringBuilder();
             sql.Append("update " + Prefix + "Standard set [StandardName] = @StandardName,[ShowWay] = @ShowWay,[StandardOtherName] = @StandardOtherName,[StandardType] = @StandardType,[StandardDetail] = @StandardDetail where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@StandardName",obj.StandardName),
							new SqlParameter("@ShowWay",obj.ShowWay),
							new SqlParameter("@StandardOtherName",obj.StandardOtherName),
							new SqlParameter("@StandardType",obj.StandardType),
							new SqlParameter("@StandardDetail",obj.StandardDetail)
                                };
             return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public int Delete(Model.CNVP_Standard obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "Standard where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
						
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public Model.CNVP_Standard LoadById(int ID)
        {
            
            StringBuilder sql = new StringBuilder();
            sql.Append("select [StandardName],[ShowWay],[StandardOtherName],[StandardType],[StandardDetail] from " + Prefix + "Standard");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
						
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            Model.CNVP_Standard cnvp_standard = new Model.CNVP_Standard();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_standard.StandardName = dr["StandardName"] is DBNull ? String.Empty : (string)dr["StandardName"];
                cnvp_standard.ShowWay = dr["ShowWay"] is DBNull ? (byte)0 : (byte)dr["ShowWay"];
                cnvp_standard.StandardOtherName = dr["StandardOtherName"] is DBNull ? String.Empty : (string)dr["StandardOtherName"];
                cnvp_standard.StandardType = dr["StandardType"] is DBNull ? (byte)0 : (byte)dr["StandardType"];
                cnvp_standard.StandardDetail = dr["StandardDetail"] is DBNull ? String.Empty : (string)dr["StandardDetail"];
            }
            return cnvp_standard;
        }

        #endregion

        public DataTable GetPage(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string sql_Temp = "";
                if (SqlCondition[0].value.ToString() != "")
                {
                    sql_Temp += " and ID in (" + SqlCondition[0].value.ToString() + ")";
                }
                string AllFields = " ID, StandardName, ShowWay, StandardOtherName, StandardType, StandardDetail";
                string Condition = "" + Prefix + "Standard where 1=1" + sql_Temp + "";
                string IndexField = "ID";
                string OrderFields = "order by Id Desc";
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }

        public DataTable GetTableBySqlWhere(string sqlWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[StandardName],[ShowWay],[StandardOtherName],[StandardType],[StandardDetail] from " + Prefix + "Standard " + sqlWhere + "");
            return DbHelper.ExecuteTable(sql.ToString());
        }

    }
}

